package com.team7.system.controller;

import com.team7.common.entity.User;
import com.team7.common.utils.WebUtil;
import com.team7.common.vo.CommonResult;
import com.team7.common.vo.UserVo;
import com.team7.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @program: team7-Object
 * @description:
 * @author: 杨海涛
 * @create: 2021-10-21 17:30
 **/
@RestController
@RequestMapping("/system/user")
@Api("用户user的api接口")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @ApiOperation("根据UserName查询用户，，用于单点登录")
    @GetMapping("findUserByUserName/{username}")
    public User findUserByUserName(@PathVariable("username") String username){
        return userService.findUserByUserName(username);
    }

    @ApiOperation("获取用户信息")
    @PostMapping("getUserByPage/{page}/{limit}")
    public CommonResult getUserByPage(@PathVariable("page") Integer page, @PathVariable("limit") Integer limit, @RequestBody UserVo userVo){
        return userService.findUserByPage(page,limit,userVo);
    }

    @ApiOperation("根据id具有的用户权限")
    @GetMapping("findUserPermissionById/{userId}")
    public List<String> findUserPermissionById(@PathVariable("userId") String userId){
        return userService.findUserPermissionById(userId);
    }

    @ApiOperation("根据用户id设置用户状态")
    @PostMapping("updateUserStatusByid/{id}/{deleted}")
    public CommonResult updateUserStatusByid(
            @PathVariable("id") String id,
            @PathVariable("deleted") Integer deleted
    ){
        return userService.updateUserStatusByid(id,deleted);
    }

    @ApiOperation("根据user对象添加用户")
    @PostMapping("addUserByUser")
    public CommonResult addUserByUser(
            @RequestBody User user
    ){
        User user1 = findUserByUserName(user.getUsername());
        if (user1!=null){
            return new CommonResult(5000,"用户已存在",null);
        }
        return userService.addUserByUser(user);
    }

    @ApiOperation("修改用户信息")
    @PostMapping("updateUserByUser")
    public CommonResult updateUserByUser(
            @RequestBody User user
    ){
        return userService.updateUserByUser(user);
    }

    @ApiOperation("根据id删除用户")
    @DeleteMapping("deleteUserById/{id}")
    public CommonResult deleteUserById(
            @PathVariable("id") String id
    ){
        return userService.deleteUserById(id);
    }

    @PostMapping("loginout")
    public CommonResult loginOutUser(){
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        redisTemplate.delete(token);
        return new CommonResult(2000,"退出成功",null);
    }


    @GetMapping("/updateUserPassword/{oldPassword}/{newPassword}")
    public CommonResult updateUserPassword(@ApiParam("登录者的账号和密码")@PathVariable("oldPassword")String oldPassword, @PathVariable("newPassword")String newPassword){
        return userService.updateUserPassword(oldPassword,newPassword);
    }
}
